<template>
	<view class="role_main">
		<scroll-view :refresher-triggered="triggered" refresher-background="transparent"
			@refresherpulling="refresherpulling" refresher-enabled="true" @refresherrefresh="onRefresh" scroll-y="true"
			class="sele_ul">
			<uni-list :border="false" v-if="networkList" class="box_list">
				<uni-list-item v-for="(item,index) in networkList" :key="index" @click="chackListItem(item)"
					style="padding: 12rpx 0;box-sizing: border-box;" clickable
					:title="item.customer?item.customer:item.network" :note="item.customer?item.network:''">
					<template v-slot:footer>
						<view class="tag-view">

							<view v-if="item.roleType==1">
								超管
							</view>
							<view v-else-if="item.roleType==2">
								管理员
							</view>
							<view v-else>
								工程师
							</view>
							<view class="tag_tip" v-if="item.code==isRole">
								<uni-icons type="checkmarkempty" size="20" color="#00c0c0"></uni-icons>
							</view>
						</view>
					</template>
				</uni-list-item>
			</uni-list>
		</scroll-view>
	</view>
</template>

<script lang="ts" setup>
	import {
		ref,
		onMounted
	} from "vue";
	import { onLoad } from '@dcloudio/uni-app'
	import store from '@/store_network/index.js'
	import apiUser from '@/util/network_api/user.js'
	const networkList = ref([])
	const isRole = ref(uni.getStorageSync('dd-access-network').isRole)
	const triggered = ref(false)
	// 获取服务商列表
	const getNetworkList = async () => {
		await apiUser.getNetworkInfo().then(res => {
			if (res.code === 20000) {
				networkList.value = res.data.rows.networks
				if (res.data.rows.networks.length == 1 && !isRole.value) {
					chackListItem(networkList.value[0])
				}
			}
		})
	}
	onLoad(() => {
		getNetworkList()
	})
	// 选择服务商
	const chackListItem = async (item) => {
		let dataValue = uni.getStorageSync('dd-access-network')
		let networkCode = ''
		uni.setStorageSync('dd-access-network', { ...dataValue, isRole: item.code })

		if (!item.networkCode) {
			networkCode = ''
		} else {
			networkCode = item.networkCode
		}
		uni.showLoading({
			title: '加载中'
		})
		await apiUser.getNetwork({ networkCode: networkCode }).then(async res => {
			if (res.code === 20000) {
				// 获取用户信息
				await store.dispatch('getUpdataUser')
				uni.reLaunch({
					url: '/pages/pages_network/home/home'
				})
				store.commit('set_isRole', item.code)
			}
		})
		uni.hideLoading()
	}
	// 下拉刷新
	const refresherpulling = (e) => {
		if (e.detail.deltaY < 0) return // 防止上滑页面也触发下拉
		triggered.value = true
	}
	const onRefresh = async () => {
		await getNetworkList()
		setTimeout(() => {
			triggered.value = false
		}, 500)
	}
</script>

<style lang="scss" scoped>
	.role_main {
		height: 100vh;
		background-color: #fafafa;
		overflow: hidden;

		.sele_ul {
			height: 100%;
			overflow: auto;

			:deep(.box_list) {
				box-sizing: border-box;
				margin: 24rpx;
				padding-bottom: 200rpx;

				.uni-list-item__content-title {
					font-size: 32rpx;
				}

				.uni-list-item__content-note {
					font-size: 28rpx;
				}

				.tag-view {
					display: flex;
					justify-content: space-between;
					align-items: center;
					flex-direction: row-reverse;
					width: 200rpx;
					font-size: 28rpx;
					color: #007cff;

					.tag_tip {
						width: 40rpx;
						height: 30rpx;
						line-height: 30rpx;
					}
				}
			}
		}
	}
</style>